package util;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 *
 * @author Airton
 *
 */
public class MaiusculaLimitadaAsg  extends PlainDocument {

	private int tamanhoMaximoString;

	// Booleano opcional caso queira tornar tudo maiúsculo
	private boolean maiuscula = false;

	public MaiusculaLimitadaAsg(int tamanhoMaximoString) {
		super();
		this.tamanhoMaximoString = tamanhoMaximoString;
	}

	public MaiusculaLimitadaAsg(int tamanhoMaximoString, boolean maiuscula) {
		super();
		this.tamanhoMaximoString = tamanhoMaximoString;
		this.maiuscula = maiuscula;
	}

	public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
		if (str == null) return;

		if ((getLength() + str.length()) > tamanhoMaximoString){
			str = str.substring(0,tamanhoMaximoString - getLength());
		}
		if ((getLength() + str.length()) <= tamanhoMaximoString) {
			if (maiuscula){
				str = str.toUpperCase();
			}
			super.insertString(offset, str, attr);
		}
	}
}
